/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import olympicgameslogic.*;

/**
 *
 * @author fabio mendonca
 */
public class DialogManageAthlete extends JDialog {

   private JTextField txtAtName = new JTextField(15);
   private JComboBox comboSex = new JComboBox(Athlete.GENDER.values());
   private Athlete athlete;
   private ImageIcon img;
   private String title;
   private Boolean trigger = false;
   ClassLoader cldr = this.getClass().getClassLoader();
   DialogManageDelegation dialogManage;
   private Delegation delegation;

   public DialogManageAthlete(Frame father, String titulo, Athlete at, DialogManageDelegation da, Delegation del) {
      super(father, titulo, true);
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      if (at != null) {
	 athlete = at;
	 img = new ImageIcon(cldr.getResource("images/extra/user_edit.png"));
	 title = "Edit Athlete";

      } else {
	 img = new ImageIcon(cldr.getResource("images/extra/user_add.png"));
	 title = "Add Athlete";
      }
      this.dialogManage = da;
      this.delegation = del;

      setLayout(new BorderLayout());
      setBackground(Color.white);

//     Criação da barra de topo
      JPanel pannorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
      pannorte.setSize(200, 60);
      pannorte.setBackground(Color.white);
      JLabel background = new JLabel(title);
      background.setFont(new Font("Arial", Font.BOLD, 14));
      background.setIcon(img);
      pannorte.add(background);

// criação dos campos             
      JPanel pancentro = new JPanel();
      pancentro.setBackground(Color.white);
      pancentro.setLayout(new GridLayout(2, 1));

//campo username        
      JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      p1.add(new JLabel("Name:"));
      p1.add(txtAtName);
      p1.setBackground(Color.white);
      pancentro.add(p1);
// tipo de utilizador    Male/female    
      JPanel p0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      p0.add(new JLabel("Sexo:"));
      comboSex.setSelectedIndex(-1);
      p0.add(comboSex);
      p0.setBackground(Color.white);
      pancentro.add(p0);

      if (athlete == null) {
	 comboSex.setSelectedItem(0);
	 txtAtName.setText("");
      } else {
	 comboSex.setSelectedItem(athlete.getSex());
	 txtAtName.setText(athlete.getName());
      }

//painel com botoes        
      JPanel pansul = new JPanel();
      pansul.setSize(200, 20);
      pansul.setBackground(Color.white);
      JButton btok = new JButton("OK", new ImageIcon(cldr.getResource("images/extra/btok.png")));
      pansul.setLayout(new FlowLayout(FlowLayout.CENTER));
      pansul.add(btok);


      add(pannorte, BorderLayout.NORTH);
      add(pancentro, BorderLayout.CENTER);
      add(pansul, BorderLayout.SOUTH);
      setSize(340, 180);
      setResizable(false);
      setLocationRelativeTo(null);

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);



      btok.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (txtAtName.getText().equalsIgnoreCase("")) {
	       JOptionPane.showMessageDialog(DialogManageAthlete.this, "Please fill Name", getTitle(), JOptionPane.WARNING_MESSAGE);
	       txtAtName.requestFocus();
	       return;
	    } else if (comboSex.getSelectedIndex() < 0) {
	       JOptionPane.showMessageDialog(DialogManageAthlete.this, "Please select Gender", getTitle(), JOptionPane.WARNING_MESSAGE);
	       comboSex.requestFocus();
	       return;
	    } else {



	       if (athlete == null) {
		  Athlete atTemp;
		  atTemp = OlympicGamesGui.ap.getAthlete(txtAtName.getText().trim(), delegation.getCountry());
		  if (atTemp == null) { //n existe -> criar e adicionar a delegação e geral
		     atTemp = new Athlete(txtAtName.getText(), Athlete.genders[comboSex.getSelectedIndex()], delegation.getCountry());
		     delegation.addAthlete(atTemp);
		     OlympicGamesGui.ap.addAthlete(atTemp);
		     JOptionPane.showMessageDialog(DialogManageAthlete.this, "Athelete added", getTitle(), JOptionPane.INFORMATION_MESSAGE);
		  } else if (!delegation.getAthleteList().contains(atTemp)) { //existe configurado por isso temos que ver se tb ja esta na delegaçao

		     delegation.addAthlete(atTemp);
		     JOptionPane.showMessageDialog(DialogManageAthlete.this, "Athelete already exists and was added to this delegation", getTitle(), JOptionPane.INFORMATION_MESSAGE);

		  } else {
		     JOptionPane.showMessageDialog(DialogManageAthlete.this, "Athelete already exists in this delegation\n Review data!", getTitle(), JOptionPane.INFORMATION_MESSAGE);
		     txtAtName.requestFocus();
		     return;

		  }
		  
		  //Edit an athlete -> athlete
	       } else {
		  Athlete atTemp;
		  atTemp = OlympicGamesGui.ap.getAthlete(txtAtName.getText().trim(), delegation.getCountry());

		  if (atTemp == null) {
		     athlete.setName(txtAtName.getText());
		     athlete.setSex((Athlete.GENDER) comboSex.getSelectedItem());
		  } else {
		     JOptionPane.showMessageDialog(DialogManageAthlete.this, "Athelete " + txtAtName.getText().trim() + " already exists!", getTitle(), JOptionPane.WARNING_MESSAGE);
		     txtAtName.requestFocus();
		     return;

		  }





	       }

	       dialogManage.refreshTable();

	       dispose();
	    }
	 }
      });







   }

   public void showJan() {
      setVisible(true);


   }
}
